package com.grj.park.test;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ParkingController {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @GetMapping("/apply/{userId}")
    public String startParkingApplication(@PathVariable String userId) {
        Map<String, Object> variables = new HashMap<>();
        variables.put("userId", userId);
        runtimeService.startProcessInstanceByKey("parkingApplication", variables);
        return "停车申请已提交";
    }

    @GetMapping("/say/{taskId}/{approved}")
    public String completeApplication(@PathVariable String taskId, @PathVariable boolean approved) {
        Map<String, Object> variables = new HashMap<>();
        variables.put("approved", approved);
        taskService.complete(taskId, variables);
        return "申请已处理";
    }

    @GetMapping("/get-tasks")
    public String getTasks() {
        taskService.createTaskQuery().processDefinitionKey("parkingApplication").list().forEach(task -> {
            System.out.println(task.getId());
            System.out.println("任务ID：" + task.getId());
            System.out.println("任务名称：" + task.getName());
            System.out.println("任务办理人：" + task.getAssignee());
            System.out.println("创建时间：" + task.getCreateTime());
            System.out.println();
            System.out.println("======================================");
        });
        return "查询成功";
    }
}
